
#include "util/log.h"
#include "server.h"
#include "eventloop.h"
#include <iostream>
using namespace std;
using namespace dong;

class servlet1:public Servlet
{
public:
    int onConnect(const Session *sess) override {
        auto lk = sess->link;
        log_info("%s",lk->remote_ip);
        return  0;
    }

    int onMessage(const Session *sess) override {
        auto lk = sess->link;
        string str = lk->copyMessage();
        return   lk->output.append(str).length();
    }

};
class servlet2:public Servlet
{
public:
    int onConnect(const Session *sess) override {
        auto lk = sess->link;
        log_info("%s",lk->remote_ip);
        return  0;
    }

    int onMessage(const Session *sess) override {
        auto lk = sess->link;
        string str = lk->copyMessage();
        return   lk->output.append(str).length();
    }

    int onClose(const Session *sess) override {
        log_info("Session is close, IP address is %s" ,sess->link->remote_ip);
    }

};
int main(int argc, char **argv){

    const char *ip = "127.0.0.1";
    int port = 8020;
    dong::Eventloop loop;
    dong::Server *serv =  new dong::Server(&loop ,ip, port);
    servlet1 svlet;
    servlet2 servlet21;
    serv->addServlet(&svlet);
    serv->addServlet(&servlet21);
    serv->start();

    loop.loop();

    return 0;
}